package com.wgts.wgts.mvp.view.activity.mycenter;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.dialog.ToastDate;
import com.wgts.wgts.control.dialog.ToastDialogFragment;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerCollectActivityComponent;
import com.wgts.wgts.dagger.module.CollectActivityModule;
import com.wgts.wgts.mvp.presenter.ICollectActivityPresenter;
import com.wgts.wgts.mvp.view.ICollectActivityView;
import com.wgts.wgts.netmodle.resp.CollectListEntity;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *
 *   data:2018/9/2911:13
 *
 */public class CollectActivity extends BaseActivity implements ICollectActivityView {
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.collect_recycle)
    RecyclerView collectRecycle;
    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;
    @BindView(R.id.net_error_ly)
    LinearLayout netErrorLy;

    private CollectRecycleAdapter adapter;

    @Inject
    ICollectActivityPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_center_collect_activity);
        ButterKnife.bind(this);
        includeTitle.setText("我的收藏");
        includeText.setText("清空过期商品");
        initView();
    }

    private void initView() {
        showDialog();
        presenter.collectList(0, 20);
    }

    private void cancelCollect() {
        adapter.setCancelClickListener(new CollectRecycleAdapter.cancelClickListener() {
            @Override
            public void cancelClick(View view, int position) {
                presenter.cancelCollect(position);
            }
        });
    }

    @Override
    public void init() {
        DaggerCollectActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .collectActivityModule(new CollectActivityModule(this))
                .build()
                .inject(this);
    }

    @OnClick({R.id.back_btn_ly, R.id.include_right_ly, R.id.net_error_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                finish();
                break;
            case R.id.include_right_ly:
                presenter.haveLoseCollect();
                break;
            case R.id.net_error_ly:
                break;
        }
    }

    @Override
    public void onError(int status, String msg) {
        if (status == 404){
            netErrorLy.setVisibility(View.VISIBLE);
            noneDataLy.setVisibility(View.GONE);
        }
    }

    @Override
    public void collectList(List<CollectListEntity.List> lists) {
        dialogDismiss();
        if (lists.size() > 0){
            noneDataLy.setVisibility(View.GONE);
            netErrorLy.setVisibility(View.GONE);
        }else {
            noneDataLy.setVisibility(View.VISIBLE);
            netErrorLy.setVisibility(View.GONE);
        }
        collectRecycle.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CollectRecycleAdapter(this, lists);
        collectRecycle.setAdapter(adapter);
        cancelCollect();
    }

    @Override
    public void deleteCollect() {
        presenter.collectList(0, 20);
    }

    @Override
    public void haveLoseCollect(final String productIds) {
        if (productIds.equals("")) {
            MoHuToast.showToast("没有过期商品");
        } else {
            ToastDate toastDate = new ToastDate();
            toastDate.setCancelQuestion("确认清空所有过期商品？");
            toastDate.setCancelText("取消");
            toastDate.setPointText("确认");
            final ToastDialogFragment fragment = ToastDialogFragment.setToastMessage(toastDate);
            fragment.show(getSupportFragmentManager(), "CancelCollectDialog");
            fragment.setOnClickListener(new ToastDialogFragment.OnClickListener() {
                @Override
                public void onClickCancle() {
                    showDialog();
                    presenter.deleteCollect(productIds);
                    fragment.dismiss();
                }

                @Override
                public void onClickEnsuer() {
                    fragment.dismiss();
                }
            });
        }
    }
}
